package com.lou1052.core.util.serialization;

import com.lou1052.core.constant.tlocal.ThreadLocalKryo;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created with IntelliJ IDEA.
 * User: liuqing
 * Date: 2015/10/18
 * Time: 15:46
 * To change this template use File | Settings | File Templates.
 */
public final class KryoSerializerUtil {
    private KryoSerializerUtil(){}

    /**
     * 序列化对象
     * @param obj
     * @return
     */
    public static  byte[] serializer(Object obj) {
        return ThreadLocalKryo.LOCAL_POOL_KRYO.get().writeObject( obj);
    }

    /**
     * 反序列化对象
     * @param data
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T deserializer(byte[] data, Class<T> clazz){
        return ThreadLocalKryo.LOCAL_POOL_KRYO.get().readObject(data, clazz);
    }

    /**
     * 从输入流中读取对象
     * @param stream
     * @param <T>
     * @return
     */
    public static <T>T read(InputStream stream,Class<T> clazz){
        return ThreadLocalKryo.LOCAL_POOL_KRYO.get().readObject(stream, clazz);
    }

    /**
     * 写入对象
     * @param stream
     * @param toWrite
     */
    public static void write(OutputStream stream, Object toWrite){
        ThreadLocalKryo.LOCAL_POOL_KRYO.get().writeObject(stream, toWrite);
    }
}
